﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace StockholmsStad.Web.UI.Test
{
    /// <summary>
    /// Summary description for DateTest
    /// </summary>
    [TestClass]
    public class DateTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //

        #endregion

        private DateTime? GetFirstYearAndDayMatch(DateTime date, IEnumerable<DateTime> dates)
        {
            DateTime? returnValue = (from d in dates
                                     where d.Year == date.Year && d.DayOfYear == date.DayOfYear
                                     select (DateTime?) d).FirstOrDefault();
            return returnValue;
        }

        [TestMethod]
        public void TestMethod1()
        {
            var date = new DateTime(2008, 01, 01);
            IList<DateTime> list = new List<DateTime>
                                       {
                                           date,
                                           new DateTime(2008, 02, 02),
                                           new DateTime(2008, 01, 01, 14, 15, 16),
                                       };
            Assert.AreEqual(date, GetFirstYearAndDayMatch(date, list).Value);
            var date2 = new DateTime(2008, 04, 01, 15, 15, 15);
            var date3 = new DateTime(2008, 04, 01, 16, 16, 16);
            IList<DateTime> list2 = new List<DateTime>
                                        {
                                            date2,
                                            new DateTime(2008, 02, 02),
                                            new DateTime(2008, 01, 01, 14, 15, 16),
                                        };
            Assert.AreEqual(date2, GetFirstYearAndDayMatch(date3, list2));
            var date4 = new DateTime(2009, 01, 01);
            Assert.IsNull(GetFirstYearAndDayMatch(date4, list2));
        }
    }
}